package com.onetech.adrewards.free.fragments;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.FeedbackOptionActivity;
import com.onetech.adrewards.free.adapters.ListNewsAdapter;
import com.onetech.adrewards.free.customviews.LoadMoreListView;
import com.onetech.adrewards.free.models.NewsModel;
import com.onetech.adrewards.free.objects.NewsDTO;

public class ListNewsFragment extends BaseAdsFragment implements OnItemClickListener{
	private ListNewsAdapter mAdapter;
	private GetListNews mGetListNews=null;
	private View mFeedbackBtn=null;
	private View mCurrentViewSelected=null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {		
		if (mView == null) {
			mView = inflater.inflate(R.layout.list_news_fragment_layout,
					container, false);			
			initView();
			initListener();
		} else {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}
	private void initView(){
		mListView=(LoadMoreListView)mView.findViewById(R.id.myListView);
		mListView.setVisibility(View.GONE);
		mAdapter=new ListNewsAdapter(getActivity());
		mListView.setAdapter(mAdapter);		
		mFeedbackBtn=(RelativeLayout)mView.findViewById(R.id.feedbackBtn);
		initNotFoundAndLoadingView();
	}
	private void initListener(){
		mFeedbackBtn.setOnClickListener(this);
		initRetryButtonListener();
		mListView.setOnLoadMoreListener(this);
		mListView.setOnItemClickListener(this);
	}
	@Override
	public void onLoadMore() {
		super.onLoadMore();
		getListNews();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.feedbackBtn:
//			FeedbackDialog mFeedbackDialog = new FeedbackDialog(getActivity());
//			mFeedbackDialog.show();
			Intent mFeedbackIntent=new Intent(getActivity(), FeedbackOptionActivity.class);
			startActivity(mFeedbackIntent);
			break;
		case R.id.btnRetry:
			pageNo=1;
			getListNews();
			break;
		default:
			super.onClick(v);
			break;
		}
		
	}
	@Override
	public void onResume() {
		super.onResume();
		if(mAdapter!=null && mAdapter.getCount()<1){
			getListNews();
		}
	}
	private void getListNews(){
		if(mGetListNews==null || mGetListNews.getStatus()==Status.FINISHED){
			mGetListNews=new GetListNews();
			mGetListNews.execute();
		}
	}
	private class GetListNews extends AsyncTask<Void, Void, List<NewsDTO>>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showNotFoundOrLoadingView(false, true);
		}
		@Override
		protected List<NewsDTO> doInBackground(Void... params) {
			return NewsModel.getInstance().getListNews(String.valueOf(pageNo));
		}
		@Override
		protected void onPostExecute(List<NewsDTO> result) {
			super.onPostExecute(result);
			if(result!=null && result.size()>0){
				if(pageNo==1){
					mAdapter.updateAppList(result);
				}
				else{
					mAdapter.insertAppList(result);
				}
				pageNo++;
			}
			if(mAdapter.getCount()<1){
				showNotFoundOrLoadingView(false, false);
				mListView.setVisibility(View.GONE);
			}else{
				showNotFoundOrLoadingView(false, false);
				mListView.setVisibility(View.VISIBLE);
			}
			mListView.onLoadMoreComplete();
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		try {
			if(position>mAdapter.getCount()-1){
				return;
			}
			NewsDTO item=mAdapter.getItem(position);
			if(item==null){
				return;
			}
			if(mCurrentViewSelected!=null){
				mCurrentViewSelected.setBackgroundColor(Color.WHITE);
			}
			view.setBackgroundColor(getActivity().getResources().getColor(R.color.bright_gray));
			mCurrentViewSelected=view;
			
			TextView mContent=(TextView) view.findViewById(R.id.charge_type);
			ImageView mIconNavi=(ImageView)view.findViewById(R.id.newsImage);
			item.setIsToogle(item.IsToogle()?false:true);
			if(!item.IsToogle()){						
				if(mContent.getLineCount()>2){					
					mIconNavi.setVisibility(View.GONE);					
				}
				mContent.setLines(mContent.getLineCount());
			}else{
				//mContent.setSingleLine(item.IsToogle());
				if(mContent.getLineCount()>2){	
					mIconNavi.setVisibility(View.VISIBLE);
					mContent.setLines(2);
				}else{
					mContent.setLines(1);
				}
				
			}			
			
//			NewsDetailDialog mNewsDetialDialog=new NewsDetailDialog(getActivity(), item);
//			mNewsDetialDialog.show();
		} catch (Exception e) {
			return;
		}
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, getActivity().getString(R.string.ListNewsFragment));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
